package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CheckedOffList.ListContent;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.text.ParseException;
import java.util.HashMap;

/**
 * Created by wu on 2016/4/8.
 * 已申请调休详情
 */
public class CheckedOffdetailFragment extends Fragment implements View.OnClickListener {

  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.CheckedOffdetailFragment.EXTRA_VALUE";
  private ListContent mContent;
  private TextView mtvOffReasonCheckedOff;//调休事由
  private TextView mtvCheckOffDaysCheckedOff;//调休天数
  private TextView mtvRelateTripCheckedOff;//关联差旅单
  private TextView mtvOffTypeCheckedOff;//调休类型
  private TextView mtvApplicatorOffCheckedOff;//调休申请人
  private TextView mtvApplicatorTimeCheckedOff;//申请日期
  private TextView mtvFileCheckedOff;//附件

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.hasCheckOffDetail);
    mContent = (ListContent) getArguments().getSerializable(EXTRA_VALUE);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_checkedoff_detail, container, false);
    mtvOffReasonCheckedOff = (TextView) view.findViewById(R.id.tv_offReason_checkedOff);
    mtvCheckOffDaysCheckedOff = (TextView) view.findViewById(R.id.tv_checkOffDays_checkedOff);
    mtvRelateTripCheckedOff = (TextView) view.findViewById(R.id.et_relateTrip_checkedOff);
    mtvOffTypeCheckedOff = (TextView) view.findViewById(R.id.tv_offType_checkedOff);
    mtvApplicatorOffCheckedOff = (TextView) view.findViewById(R.id.tv_applicatorOff_checkedOff);
    mtvFileCheckedOff = (TextView) view.findViewById(R.id.tv_file_checkedOff);
    mtvApplicatorTimeCheckedOff = (TextView) view.findViewById(R.id.tv_applicatorTime_checkedOff);

    initData();

    return view;
  }

  private void initData() {
    mtvOffReasonCheckedOff.setText(mContent.getHolyDesc());
    mtvCheckOffDaysCheckedOff.setText(mContent.getHolyDays());
    mtvRelateTripCheckedOff.setText(mContent.getBtripListName());

    //调休类型
    HashMap<String, String> map = CheckListFragment.mapOffType;
    String kindName = map.get(mContent.getHolyKindCode());
    mtvOffTypeCheckedOff.setText(kindName);

    mtvApplicatorOffCheckedOff.setText(mContent.getStaffName());
    mtvFileCheckedOff.setText(mContent.getFileOriginalName());
    try {
      mtvApplicatorTimeCheckedOff.setText(
          MyDateUtil.dateToString(mContent.getRegDate(), MyDateUtil.TYPE_DATE));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    mtvRelateTripCheckedOff.setOnClickListener(this);//差旅单
    mtvFileCheckedOff.setOnClickListener(this);//附件
  }

  public static Fragment newInstance(ListContent content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, content);
    CheckedOffdetailFragment fragment = new CheckedOffdetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        //                if (NavUtils.getParentActivityName(getActivity()) != null) {
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onClick(View v) {

    switch (v.getId()) {
      case R.id.et_relateTrip_checkedOff:
        String id = mContent.getBtripListCode();
        if (TextUtils.isEmpty(id)) {
          ToastUtil.show(R.string.noRelateTrip);
        } else {
          Intent intent = new Intent(getActivity(), CheckedOffTripDetailActivity.class);
          intent.putExtra(CheckedOffTripDetailFragment.EXTRA_ID, id);
          startActivity(intent);
        }

        break;
      case R.id.tv_file_checkedOff:
        String mFileUrl = mContent.getFilePath();
        if (!TextUtils.isEmpty(mFileUrl)) {
          ExtraDownLoadDialogFragment dialog =
              ExtraDownLoadDialogFragment.newInstance("", mFileUrl);
          FragmentManager fm = getActivity().getSupportFragmentManager();
          dialog.show(fm, "");
        }
        break;
      default:
        break;
    }
  }
}
